"""
配置变量定义
"""

from pathlib import Path

import yaml

from . import config_version, config_path


def merge_dicts(base: dict, extend: dict):
    """在基础字典(base)的基础上更新扩展字典(extend)的kv"""
    for key, value in extend.items():
        if key in base and isinstance(base[key], dict) and isinstance(value, dict):
            merge_dicts(base[key], value)
        else:
            base[key] = value


def load_config(version: str = config_version) -> dict:
    """读取配置文件"""
    current_file = Path(__file__).resolve()
    config_directory = current_file.parent / version
    base = dict()

    pattern = f'{config_path}/*.yaml'
    for file in config_directory.glob(pattern):
        with file.open('r', encoding='utf-8') as f:
            extend = yaml.safe_load(f)
            if file.name == 'config.yaml':
                base, extend = extend, base
            merge_dicts(base, extend)
    # print("读取配置:", base["project"]["name"])
    return base
